package com.hq.bus.controller;

import com.hq.bus.domain.Check;
import com.hq.bus.domain.Customer;
import com.hq.bus.domain.Rent;
import com.hq.bus.service.CarService;
import com.hq.bus.service.CheckService;
import com.hq.bus.service.CustomerService;
import com.hq.bus.service.RentService;
import com.hq.bus.vo.CarVo;
import com.hq.bus.vo.CheckVo;
import com.hq.bus.vo.RentVo;
import com.hq.sys.constat.SysConstant;
import com.hq.sys.util.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
/**
 * @BelongsPackage: com.hq.bus.controller
 * @Author: jiangjunjie
 * @CreateTime: 2024-12-18  15:40
 * @Description: TODO
 * @Version: 1.8
 */
@RestController
@RequestMapping("check")
public class CheckController {
    @Autowired
    private CheckService checkService;
    @Autowired
    private RentService rentOrderService;
    @Autowired
    private CarService carService;
    @Autowired
    private CustomerService customerService;

    /**
     * 加载检查单列表返回DataGridView
     * @param checkVo 检查单查询条件
     * @return 包含检查单数据的DataGridView对象
     */
    @RequestMapping("loadAllCheck")
    public DataGridView loadAllCheck(CheckVo checkVo) {
        return this.checkService.queryAllCheck(checkVo);
    }

    /**
     * 初始化检查表单数据
     * @param check 检查单对象
     * @return 包含出租单、客户和车辆信息的Map对象
     */
    @RequestMapping("initCheckFormData")
    public Map<String, Object> initCheckFormData(Check check) {
        // 查询出租单
        Rent rent = rentOrderService.queryRentByIdentity(check.getCheckid());
        // 查询客户
        Customer customer = customerService.queryCustomerByIdentity(rent.getIdentity());
        // 查询车辆
        CarVo carVo = carService.queryCarById(rent.getCarnumber());

        // 组装check数据
        Map<String, Object> formData = new HashMap<>();
        formData.put("rent", rent);
        formData.put("customer", customer);
        formData.put("car", carVo);

        return formData;
    }

    /**
     * 保存检查信息并更新相关状态
     * @param check 检查单对象
     * @return 操作结果字符串
     */
    @RequestMapping("saveCheck")
    public String saveCheck(Check check) {
        try {
            // 判断汽车状态是否出租
            Rent rent = rentOrderService.queryRentByIdentity(check.getCheckid());
            if (carService.queryCarById(rent.getCarnumber()).getIsrenting() == 1) {
                return "汽车状态为已出租，不能进行检查";
            }

            // 更新检查单信息
            checkService.updateCheck(check);

            // 更改出租单的状态
            rent.setRentflag(Double.valueOf(SysConstant.RESET_SUCCESS));
            rentOrderService.updateRent(new RentVo(rent));

            // 更改汽车的状态
            CarVo vehicle = carService.queryCarById(rent.getCarnumber());
            vehicle.setIsrenting(0); // 假设未出租的状态码为0
            carService.updateCar(vehicle);

            return "保存检查信息以及相关状态更新成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "保存检查信息以及相关状态更新出现错误，错误原因：" + e.getMessage();
        }
    }
}